package com.lanyou.esb.repository;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import com.lanyou.esb.entity.FtpLog;
/**
 * 
 * @author ly-zengzugen
 *
 */
public interface FtpLogRepository extends Repository<FtpLog, String>, JpaSpecificationExecutor<FtpLog> {

	
	@Query(nativeQuery = true,
			value = "SELECT ts.FIELD_SYSTEM_ID, tf.FIELD_NAME, ts.SYSTEM_NAME, tfp.IF_NAME, tfp.IF_TYPE, COUNT(tl.IF_FTP_ID)"
					+ " FROM t_cook_db_field tf, t_cook_bu_field_system ts, t_esb_bu_if_ftp_log tl, t_esb_bu_if_ftp tfp"
					+ " WHERE tf.FIELD_ID = ts.FIELD_ID"
					+ " AND ts.FIELD_SYSTEM_ID = tfp.FIELD_SYSTEM_ID"
					+ " AND tl.IF_FTP_ID = tfp.IF_FTP_ID"
					+ " AND tfp.IS_ENABLE = '1'"
					+ " AND EXISTS(SELECT *FROM t_cook_bu_user_system tus WHERE tus.FIELD_SYSTEM_ID = ts.FIELD_SYSTEM_ID AND tus.USER_ID = :userId)"
					+ " AND tl.TRANSPORT_DATE BETWEEN :beginDate AND :endDate"
					+ " AND tl.TRANSPORT_RESULT = '0'"
					+ " GROUP BY tf.FIELD_NAME, ts.SYSTEM_NAME, tl.IF_FTP_ID;")
	List<Object[]> findFtpErrorInterfaces(@Param("userId") String userId,
										  @Param("beginDate") Date beginDate,
										  @Param("endDate") Date endDate);
	
	
	
										  
	long count();

	boolean exists(String id);

	@RestResource(exported = true)
	@RequiresPermissions(value={"serviceLogQuery"})
	<S extends FtpLog> S save(S entity); // could expose REST API

	<S extends FtpLog> List<S> save(Iterable<S> entities);

	@RestResource(exported = true)
    @RequiresPermissions(value={"serviceLogQuery"})
	FtpLog findOne(String id); // could expose REST API

	FtpLog getOne(String id);

	// 以下三个接口只能选一个暴露为REST API

	@RestResource(exported = false)
	List<FtpLog> findAll(); // could expose REST API

	@RestResource(exported = false)
	List<FtpLog> findAll(Sort sort); // could expose REST API

	@RestResource(exported = true)
	@RequiresPermissions(value={"serviceLogQuery"})
	Page<FtpLog> findAll(Pageable pageable); // could expose REST API

	List<FtpLog> findAll(Iterable<String> ids);

	// 以下两个接口，要么都暴露为REST API，要么都不暴露
	@RestResource(exported = true)
	@RequiresPermissions(value={"serviceLogQuery"})
	void delete(String id); // could expose REST API

	@RestResource(exported = true)
	@RequiresPermissions(value={"serviceLogQuery"})
	void delete(FtpLog entity); // could expose REST API

	void delete(Iterable<? extends FtpLog> entities);

	void deleteAll();

	void flush();

	<S extends FtpLog> S saveAndFlush(S entity);

	void deleteInBatch(Iterable<FtpLog> entities);

	void deleteAllInBatch();

}
